import numpy as np
import csv
import random
from datetime import datetime
from time import strftime

'''
revision of test24
to calculate the false nagetive, false positive

'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey


#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()
    inputCSV = "C:/_DATA/CancerData/test/Mar21/pvalue/16_4polygon/hierarchy/WARD_SO_10000_cancer27_pvalue.csv"
    dataMatrix = build_data_list(inputCSV)
    iLen = dataMatrix.shape
    p_value_length = int((iLen[1] - 1)/4)
    unit_attri = np.zeros((iLen[0],p_value_length))
    p_value = np.zeros((iLen[0],2))
    p_value[:,0] = dataMatrix[:,0]
    for i in range(p_value_length):
        unit_attri[:,i] = dataMatrix[:,(i*4+1)]
    i = 0
    for item in unit_attri:
        p_value[i,1] = np.min(item)
        i += 1
    filePath = inputCSV[:-4] + "_total.csv"
    np.savetxt(filePath, p_value, delimiter=',')

    print "end at " + getCurTime()
    print "========================================================================"  

           
